<template>
  <ele-page>
    <!-- 搜索表单 -->
    <!-- <search @search="reload" :job-id="jobId" ref="searchRef" /> -->
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        row-key="id"
        v-model:selections="selections"
        row-click-checked
        stripe
        border
        :pagination="false"
      >
        <template #toolbar>
          <el-button type="success" class="ele-btn-icon" @click="openAdd()">
            添加
          </el-button>
        </template>

        <template #action="{ row }">
          <el-link type="danger" :underline="false" @click="remove(row)">
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 添加报名记录 -->
    <student v-model="showAdd" :job="job" @done="reload" @close="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Student from './student/index.vue';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import { pageMpMessageUser, removeMpMessageUser } from '@/api/mall';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'member_id',
      label: 'id',
      width: 95
    },
    {
      prop: 'realname',
      label: '名字',
      width: 95
    },
    {
      prop: 'tel',
      label: '电话',
      width: 122
    },

    {
      label: '操作',
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 添加推送用户 */
  const showAdd = ref(false);
  const openAdd = () => {
    showAdd.value = true;
  };

  /** 表格数据源 */
  const datasource = ({ page, where, orders, limit }) => {
    return pageMpMessageUser({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 删除单个 */
  const remove = (row) => {
    ElMessageBox.confirm(
      '确定要将“' + row.realname + '”移出推送列表吗?',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        removeMpMessageUser({ id })
          .then((msg) => {
            loading.close();
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>
